<?php

/**
 * Description of ControllerConsultaPadrao
 *
 * @author Cristiana Sasse
 */
abstract class ControllerConsultaPadrao extends Controller {
    /** @var ViewConsultaPadrao */
    protected $View;   
    /** @var Principal */
    protected $Principal;
    protected $sql;
    /** @var ControllerFiltroConsultaPadrao */
    protected $ControllerFiltro;

    public function __construct() {
        $this->Principal = Principal::getInstance();
        $this->setPersistencia($this->getInstancePersistencia());
        $this->setView($this->getInstanceView());
    }    

    public function run() {
        $this->sql = $this->getSqlPadraoConsulta();
        $this->aplicaFiltros();
        $this->aplicaOrdem();
        $this->trataSqlPadraoConsulta();
        $this->View->setQuery($this->Principal->getConexao()->newQuery($this->sql));
        $this->imprime();
    }
    
    protected function aplicaFiltros() {
        $this->getControllerFiltro()->aplicaFiltros($this->sql);
    }
    
    protected function aplicaOrdem() {
        $this->getControllerFiltro()->aplicaOrdem($this->sql);
    }
    
    protected function getControllerFiltro() {
        if (!$this->ControllerFiltro) {
            $this->ControllerFiltro = new ControllerFiltroConsultaPadrao($this->View->getTelaFiltro(), $this->View->getFiltroPadrao());
        }
        return $this->ControllerFiltro;
    }

    protected function getSqlPadraoConsulta() {
        return $this->getPersistencia()->getSqlPadraoConsulta();
    }

    abstract protected function getInstancePersistencia();
    
    abstract protected function getInstanceView();
    
    protected function trataSqlPadraoConsulta(){}
    
}

?>
